package com.robotlab.easytranslate.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.robotlab.easytranslate.R;
import com.robotlab.easytranslate.activity.MainActivity;
import com.robotlab.easytranslate.adapter.wordAdapter;
import com.robotlab.easytranslate.dbase.Wordbook;

import org.litepal.crud.DataSupport;
import org.w3c.dom.Text;

import java.io.IOException;
import java.util.List;


public class BookFragment extends Fragment {

    private LinearLayout ll_wordbook;
    private RelativeLayout rl_wordbook;
    private TextView tv_clear;
    private RecyclerView words;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_book, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        ll_wordbook = view.findViewById(R.id.ll_wordbook);
        rl_wordbook = view.findViewById(R.id.rl_wordbook);
        words = view.findViewById(R.id.words);
        tv_clear = view.findViewById(R.id.tv_clear);
        tv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
                dialog.setTitle("确认删除？");
                dialog.setMessage("删除后无法恢复");
                dialog.setCancelable(false);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        DataSupport.deleteAll(Wordbook.class);
                        ll_wordbook.setVisibility(View.GONE);
                        rl_wordbook.setVisibility(View.VISIBLE);

                        MainActivity mainActivity = (MainActivity)getActivity();
                        mainActivity.sendWord("--");
                        TranslateFragment.isCollect = false;
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                dialog.show();

            }
        });
        if (DataSupport.count(Wordbook.class) != 0) {
            ll_wordbook.setVisibility(View.VISIBLE);
            rl_wordbook.setVisibility(View.GONE);
        } else {
            ll_wordbook.setVisibility(View.GONE);
            rl_wordbook.setVisibility(View.VISIBLE);
        }
        final List<Wordbook> wordbooks = DataSupport.findAll(Wordbook.class);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        words.setLayoutManager(linearLayoutManager);
        wordAdapter adapter = new wordAdapter(R.layout.word_item,wordbooks);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Wordbook wordbook = wordbooks.get(position);
                try {
                    MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(wordbook.getVoiceUrlText());
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //Toast.makeText(getContext(), wordbook.getQuereText(), Toast.LENGTH_SHORT).show();
            }
        });
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final BaseQuickAdapter adapter, View view, int position) {
                final Wordbook wordbook = wordbooks.get(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("");
                //builder.setMessage(wordbook.getQuereText());
                final String[] selected = {"查看详细释义", "删除该词"};
                builder.setItems(selected, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        switch (i) {
                            case 0:
                                MainActivity mainActivity = (MainActivity)getActivity();
                                mainActivity.getResult().setSelection(1);
                                //mainActivity.translateFragment.getInput().setText("1");
                                //mainActivity.translateFragment.getIB_tran().performClick();
                                mainActivity.sendWord(wordbook.getQuereText());
                                break;
                            case 1:
                                TranslateFragment.isCollect = false;
                                DataSupport.deleteAll(Wordbook.class, "quereText = ?", wordbook.getQuereText());
                                Toast.makeText(getContext(), "已从单词本中移除", Toast.LENGTH_SHORT).show();
                                MainActivity mainActivity1 = (MainActivity)getActivity();
                                mainActivity1.getResult().setSelection(3);
                                mainActivity1.sendWord("--");
                                break;
                        }
                    }
                });
                builder.show();
                return false;
            }
        });
        words.setAdapter(adapter);
    }

    public interface sendWord{
        void sendWord(String word);
    }
}
